package com.clean.cleantotal.base

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.clean.cleantotal.R
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.bean.JunkBean
import com.clean.cleantotal.bean.JunkGroup
import com.clean.cleantotal.compores.delete.DeleteActivity
import com.clean.cleantotal.utils.FormatterSize
import com.clean.cleantotal.utils.ScanJunkUtils
import com.clean.cleantotal.utils.Utils
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup

abstract class BaseGroupActivity<T : ViewBinding> : BaseActivity<T>() {

    protected var scanJunkUtils: ScanJunkUtils? = null
    protected var selectList = HashSet<JunkBean>()
    protected var totalCount = 0
    protected var totalSize = 0L

    private lateinit var recyclerView: RecyclerView

    override fun initView() {
        scanJunkUtils = ScanJunkUtils()
        recyclerView = initGroupRecyclerView()
        recyclerView.linear().setup {
            addType<JunkGroup> { R.layout.item_group_layout }
            addType<JunkBean> { R.layout.item_clean_layout }
            onClick(R.id.groupLayout) {
                expandOrCollapse()
            }
            onClick(R.id.ivSelect) {
                when (itemViewType) {
                    R.layout.item_group_layout -> {
                        val model = getModel<JunkGroup>()
                        model.isSelect = !model.isSelect
                        model.sublist.forEach {
                            it.isChecked = model.isSelect
                        }
                        if (model.isSelect) {
                            selectList.addAll(model.sublist)
                        } else {
                            selectList.removeAll(model.sublist.toSet())
                        }
                    }

                    R.layout.item_clean_layout -> {
                        val scanBean = getModel<JunkBean>()
                        scanBean.isChecked = !scanBean.isChecked
                        if (scanBean.isChecked) {
                            selectList.add(scanBean)
                        } else {
                            selectList.remove(scanBean)
                        }
                        val parentPosition = findParentPosition()
                        if (parentPosition != -1) {
                            // 删除父item的嵌套分组数据
                            val parent = getModel<JunkGroup>(parentPosition)
                            val noChecked = (parent.sublist.filter { !it.isChecked })
                            parent.isSelect = noChecked.isEmpty()
                        }
                    }
                }
                updateUi()
            }
        }.models
        selectAllClick(allSelectView())
        clickClean(clickCleanView())
    }

    abstract fun isLargeScan(): Boolean

    abstract fun initGroupRecyclerView(): RecyclerView
    abstract fun allSelectView(): View
    abstract fun clickCleanView(): View

    protected fun initRecyclerViewData(groups: List<JunkGroup>) {
        totalCount = groups.sumOf { it.sublist.size }
        totalSize = groups.sumOf { it.totalSize }
        recyclerView.models = groups
    }

    protected var isAllSelect = false
    private fun selectAllClick(allView: View) {
        allView.setOnClickListener {
            recyclerView.bindingAdapter.models?.forEach {
                if (it is JunkGroup) {
                    it.isSelect = !isAllSelect
                    it.sublist.forEach {
                        it.isChecked = !isAllSelect
                    }
                    if (it.isSelect) {
                        selectList.addAll(it.sublist)
                    } else {
                        selectList.removeAll(it.sublist.toSet())
                    }
                } else if (it is JunkBean) {
                    it.isChecked = !isAllSelect
                    if (it.isChecked) {
                        selectList.add(it)
                    } else {
                        selectList.remove(it)
                    }
                }
            }
            updateUi()
        }
    }

    private fun clickClean(clickView: View) {
        clickView.setOnClickListener {
            Utils.cleanJunFiles.clear()
            Utils.cleanJunFiles.addAll(selectList)
            val funType = if (isLargeScan()) {
                FunType.LARGE
            } else {
                FunType.CLEAN
            }
            openActivity(DeleteActivity::class.java, Bundle().apply {
                putSerializable("funType", funType)
            })
            finish()
        }
    }

    abstract fun updateUi()
    override fun onDestroy() {
        super.onDestroy()
        scanJunkUtils?.destroy()
    }

    override fun onBackPressed() {
        Utils.showBackPop(this, FormatterSize.formatFileSize(this, totalSize), {
            super.onBackPressed()
        }) {
            clickCleanView().performClick()
        }
    }
}